﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
    public class ShopCarService
    {
        /// <summary>
        /// 编辑购物车数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Edit(TB_ShopCar entity)
        {
            //var sql = "select * from [dbo].[TB_ShopCar] where gid="+entity.Gid+" and tcid="+entity.TcId +" and UserId="+entity.UserId;//如果存在该类型的商品则修改数量
            //var row = SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
            //if (row.Rows.Count>0)
            //{
            //    entity.Id = Convert.ToInt32(row.Rows[0]["Id"].ToString());
            //}
            var sql = "";
            if (entity.Id == 0)
            {
                sql = "insert into [dbo].[TB_ShopCar](gid,tcid,[count],[userid])values('" + entity.Gid + "','" +
                      entity.TcId + "','" + entity.Count + "','" + entity.UserId + "') select @@identity";
                return Convert.ToInt32(SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql));
            }
                sql = "update [dbo].[TB_ShopCar] set [count]=" + entity.Count + " where id=" + entity.Id + " and userid=" + entity.UserId;
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql);
        }

       
        /// <summary>
        /// 获取购物车列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetList(int userId)
        {
            var sql = "select  a.Id,a.Count,b.Img1,b.Name,b.Price,b.DiscountPrice,b.Count,b.PayCountTrue," +
                      "(select Name from [dbo].[TB_Good_Property] where TB_Good_Property.Id=a.tcid) as tcname,"+
                      "(select Value from [dbo].[TB_Good_Property] where TB_Good_Property.Id=a.tcid) as tcvalue," +
                      "(select price from [dbo].[TB_Good_Property] where TB_Good_Property.Id=a.tcid) as tcprice" +
                      " from [dbo].[TB_ShopCar] as a,[dbo].[TB_Goods] as b where a.gid=b.id and a.userid=" + userId + " order by a.Id desc";
            return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
        }

        /// <summary>
        /// 修改购物车数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int EditCount(int id,int count)
        {
            string sql = "update [dbo].[TB_ShopCar] set Count="+count+" where id="+id;
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql);
        }

        /// <summary>
        /// 查询购物车数量
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int GetShopCarCount(int userid)
        {
            string sql = "select count(*) as count from  [dbo].[TB_ShopCar] where  userid="+userid;
            return Convert.ToInt32(SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql));
        }
    }
}
